package cn.zswltech.gruul.web.api.controller;

import cn.zswltech.gruul.api.SystemApi;
import cn.zswltech.gruul.biz.service.SystemConfigService;
import cn.zswltech.gruul.common.result.Response;
import cn.zswltech.gruul.common.util.StringUtil;
import cn.zswltech.gruul.common.entity.SystemConfigDO;
import cn.zswltech.gruul.common.dal.query.SystemConfigQuery;
import cn.zswltech.gruul.web.api.controller.common.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class SystemController extends BaseController implements SystemApi {

    @Resource(name = "systemConfigServiceAPI")
    private SystemConfigService systemConfigService;

    /**
     * 查询参数配置值（不校验权限）
     *
     * @return
     * @
     */
    @Override
    public Response getConfigValue(@PathVariable String key) {
        return systemConfigService.getConfigValue(key);
    }

    /**
     * 查询系统配置详情（不校验权限）
     *
     * @return
     * @
     */
    @Override
    public Response<SystemConfigDO> getConfig(HttpServletRequest request) {
        return systemConfigService.getConfig(request.getParameter("myKey"));
    }

    /**
     * 查询系统配置列表
     *
     * @return
     * @
     */
    @Override
    public Response<Map<String, Object>> getConfigList(SystemConfigQuery query) {
        return systemConfigService.getConfigList(query);
    }

    /**
     * 新建系统配置
     *
     * @param request
     * @return
     * @
     */
    @Override
    public Response createConfig(HttpServletRequest request) {
        SystemConfigDO systemConfigDO = genSystemConfigDO(request);
        return systemConfigService.createConfig(systemConfigDO);
    }

    private SystemConfigDO genSystemConfigDO(HttpServletRequest request) {
        SystemConfigDO systemConfigDO = new SystemConfigDO();
        systemConfigDO.setConfigKey(request.getParameter("configKey"));
        systemConfigDO.setConfigValue(request.getParameter("configValue"));
        systemConfigDO.setType(request.getParameter("type"));
        systemConfigDO.setStatus(StringUtil.str2Int(request.getParameter("status")));
        systemConfigDO.setDescription(request.getParameter("description"));
        return systemConfigDO;
    }

    /**
     * 编辑系统配置
     *
     * @param request
     * @return
     * @
     */
    @Override
    public Response updateConfig(HttpServletRequest request) {
        SystemConfigDO systemConfigDO = genSystemConfigDO(request);
        systemConfigDO.setId(Long.valueOf(request.getParameter("id")));
        return systemConfigService.updateConfig(systemConfigDO);
    }

    /**
     * 删除系统配置
     *
     * @param request
     * @return
     * @
     */
    @Override
    public Response deleteConfig(HttpServletRequest request) {
        Long id = Long.valueOf(request.getParameter("id"));
        return systemConfigService.deleteConfig(id);
    }
}
